<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			input{
				outline: none;
			}
			#form{
				margin-left: 50px;
			}
		</style>
	</head>
	<body>
		<form id="form">
			<br>
			<input type="text" name="name"placeholder="请输入书本名称" />
			<br><br>
			<input type="text" name="price"placeholder="单价" />
			<br><br>
			<!-- <input type="hidden" name="count" />
			<br><br> -->
			<input type="date" name="tdate"placeholder="出版日期" />
			<br><br>
			<button type="button" onclick="save()">保存</button>
		</form>
		<script type="text/javascript">
			let input_name = document.getElementsByName("name")[0];
			let input_price = document.getElementsByName("price")[0];
			let input_tdate = document.getElementsByName("tdate")[0];
			// let input_count = document.getElementsByName("tdate")[0];
			
			//判断是添加还是编辑
			//进行解析
			let item =  JSON.parse(sessionStorage.getItem("item"));
			if(item){
				input_name.value = item.name;
				input_price.value = item.price;
				input_tdate.value = item.tdate;
				// input_count.value = item.count;
			}
			
			function save(){
				// let form = document.getElementById("form")
				let name = input_name.value;
				let price = input_price.value;
				let tdate = input_tdate.value;
				let obj = {
					id: 1,
					name: name,
					tdate: tdate,
					price: price,
					//三元表达式，如果没有就是1
					// count:input_count.value ? input_count : 1
					count:item ? item.count : 1
				}
				
				//获取数组
				let data = sessionStorage.getItem("data");
				if(data){
					data = JSON.parse(data);
					if(item){
						let index = sessionStorage.getItem("index");
						data.splice(index,1,obj)
						// item.removeAttribute("index");
						// data.splice(item.index,0,item);
					}else{
						data.push(obj); // 添加
					}
					// console.log(data);
					//放回去
					sessionStorage.setItem("data",JSON.stringify(data));
					//页面跳转
					location.href = '8-06的作业讲解.html';
				}
			}
		</script>
	</body>
</html>
